package cn.edu.dlmu.cogo.framework.log;

import cn.edu.dlmu.cogo.framework.config.JavaConfig;
import cn.edu.dlmu.cogo.framework.config.LogConfig;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.lang.Console;
import cn.hutool.log.AbstractLog;
import cn.hutool.log.level.Level;

import java.io.File;

/**
 * @author yixi
 */
public class CogoLog extends AbstractLog {

    private final String loggerName;

    private static final String TEMPLATE = "%s  %s [%s] : %s\r\n";

    private File debugFile;

    private FileWriter debugWriter;

    private File errorFile;

    private FileWriter errorWriter;

    public CogoLog(String name) {
        this.loggerName = name;
        LogConfig config = JavaConfig.getConfig(LogConfig.class);
        this.debugFile = new File(config.getDebugPath() + DateUtil.today() + ".txt");
        this.errorFile = new File(config.getErrorPath() + DateUtil.today() + ".txt");
        this.debugWriter = new FileWriter(debugFile);
        this.errorWriter = new FileWriter(errorFile);
    }

    public CogoLog(Class<?> clazz){
        this(clazz == null ? "null" : clazz.getName());
    }

    public boolean isTimeOut(File logFile) {
        return !logFile.getAbsolutePath().endsWith(DateUtil.today() + ".txt");
    }

    @Override
    public String getName() {
        return this.loggerName;
    }

    @Override
    public void log(String s, Level level, Throwable throwable, String s1, Object... objects) {
        debug(s,throwable,s1,objects);
    }

    @Override
    public boolean isDebugEnabled() {
        return true;
    }

    @Override
    public void debug(String s, Throwable throwable, String s1, Object... objects) {
        Console.log(String.format(TEMPLATE,DateUtil.now(),loggerName,"DEBUG",s1),objects);
        if (isTimeOut(debugFile)){
            LogConfig config = JavaConfig.getConfig(LogConfig.class);
            debugFile = new File(config.getDebugPath() + DateUtil.today() + ".txt");
            debugWriter = new FileWriter(debugFile);
        }
        debugWriter.append(String.format(TEMPLATE,DateUtil.now(),loggerName,"DEBUG",s1));
    }

    @Override
    public boolean isErrorEnabled() {
        return true;
    }

    @Override
    public void error(String s, Throwable throwable, String s1, Object... objects) {
        Console.error(String.format(TEMPLATE,DateUtil.now(),loggerName,"ERROR",s1),objects);
        if (isTimeOut(errorFile)){
            LogConfig config = JavaConfig.getConfig(LogConfig.class);
            errorFile = new File(config.getErrorPath() + DateUtil.today() + ".txt");
            errorWriter = new FileWriter(errorFile);
        }
        errorWriter.append(String.format(TEMPLATE,DateUtil.now(),loggerName,"ERROR",s1));
    }

    @Override
    public boolean isInfoEnabled() {
        return false;
    }

    @Override
    public void info(String s, Throwable throwable, String s1, Object... objects) {
        warn(s,throwable,s1,objects);
    }

    @Override
    public boolean isTraceEnabled() {
        return false;
    }

    @Override
    public void trace(String s, Throwable throwable, String s1, Object... objects) {
        warn(s,throwable,s1,objects);
    }

    @Override
    public boolean isWarnEnabled() {
        return false;
    }

    @Override
    public void warn(String s, Throwable throwable, String s1, Object... objects) {
        Console.log(String.format(TEMPLATE, DateUtil.now(),loggerName,"WARN",s1),objects);
    }
}
